/**
 * 解法: 哈希表复杂度O(n²)
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
  // for循环nums，当target - nums[i] 在nums中存在时，返回两个数的索引
  for (let i = 0; i < nums.length; i++) {
    if (nums.includes(target - nums[i])) {
      // 判断两个索引是否相同
      if (i !== nums.indexOf(target - nums[i])) {
        return [i, nums.indexOf(target - nums[i])]
      }
    }
  }
  return []
};

// 优化解法: 哈希表复杂度O(n)
var twoSum2 = function(nums, target) {
  let map = new Map()
  for (let i = 0; i < nums.length; i++) {
    if (map.has(target - nums[i])) {
      return [map.get(target - nums[i]), i]
    }
    map.set(nums[i], i)
  }
  return []
};

let res = twoSum([3,2,4], 6)
console.log(res)